//
//  DoubleColorProgressBar.m
//  SKPAD
//
//  Created by admin@amonline.com.cn on 15/6/2.
//  Copyright (c) 2015年 amo. All rights reserved.
//


#import "TwoColorProgressBar.h"


@implementation TwoColorProgressBar
//代码创建初始化
-(id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        //黄色刻度背景图层
        _trackLayerGraduationYellow = [CAShapeLayer layer];
        _trackLayerGraduationYellow.frame = CGRectMake(0, 0, frame.size.width, frame.size.height * 0.3081);
        _trackLayerGraduationYellow.fillColor = [UIColor clearColor].CGColor;
        UIColor * darkYellow = [UIColor colorWithRed:86.0/255 green:88.0/255 blue:42.0/255 alpha:1];
        _trackLayerGraduationYellow.strokeColor = darkYellow.CGColor;
        
        //绿色刻度背景图层
        _trackLayerGraduationGreen = [CAShapeLayer layer];
        _trackLayerGraduationGreen.frame = CGRectMake(0, frame.size.height * 0.3081, frame.size.width, frame.size.height * 0.6919);
        _trackLayerGraduationGreen.fillColor = [UIColor yellowColor].CGColor;
        UIColor * darkGreen = [UIColor colorWithRed:38.0/255 green:90.0/255 blue:43.0/255 alpha:1];
        _trackLayerGraduationGreen.strokeColor = darkGreen.CGColor;
        
        //黄色刻度背景图层
        _trackLayerRealityYerrow = [CAShapeLayer layer];
        _trackLayerRealityYerrow.frame = CGRectMake(0, 0, frame.size.width, frame.size.height * 0.3081);
        _trackLayerRealityYerrow.fillColor = [UIColor clearColor].CGColor;
        _trackLayerRealityYerrow.strokeColor = [UIColor yellowColor].CGColor;
        
        //绿色刻度背景图层
        _trackLayerRealityGreen = [CAShapeLayer layer];
        _trackLayerRealityGreen.frame = CGRectMake(0, frame.size.height * 0.3081, frame.size.width, frame.size.height * 0.6919);
        _trackLayerRealityGreen.fillColor = [UIColor yellowColor].CGColor;
        _trackLayerRealityGreen.strokeColor = [UIColor greenColor].CGColor;
        
        
        UIImage * image = [UIImage imageNamed:@"trough(1).png"];
        _backgroundView = [[UIImageView alloc] initWithImage:image];
        _backgroundView.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
        _backgroundView.backgroundColor = [UIColor clearColor];
        
        UIImage * image2 = [UIImage imageNamed:@"trough(1)_grey.png"];
        _backgroundView2 = [[UIImageView alloc] initWithImage:image2];
        _backgroundView2.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
        _backgroundView2.backgroundColor = [UIColor clearColor];
        
        if(_arrayStartY == nil)
        {
            _arrayStartY = [[NSMutableArray alloc] init];
        }
        
        _minimumValue = 0.0;
        _maximumValue = 1.0;
        _value = 0.0;
        _totalDuplicate = 42;
        self.backgroundColor = [UIColor clearColor];
        [self updateTrackLayerGraduation];
        [self updateTrackLayerReality];
        [self createKnobUI];
        
    }
    return  self;
}

-(void) updateTrackLayerGraduation
{

    _trackLayerGraduationYellow.lineWidth = _backgroundView2.frame.size.height/108;
    _trackLayerGraduationGreen.lineWidth = _backgroundView2.frame.size.height/108;
    
    //刻度线
    UIBezierPath * pointerOutsideYellow = [UIBezierPath bezierPath];
    CGFloat eachValue =_backgroundView2.frame.size.height/108;
    float startY = eachValue * 3;
    BOOL isGraduation = YES;
    while (startY < _trackLayerGraduationYellow.frame.size.height)
    {
        if(isGraduation)
        {
            NSNumber * startNumberY = [NSNumber numberWithFloat:startY];
            [_arrayStartY addObject:startNumberY];
            [pointerOutsideYellow moveToPoint:CGPointMake(0, startY)];
            
            [pointerOutsideYellow addLineToPoint:CGPointMake(_trackLayerGraduationYellow.frame.size.width, startY)];
            startY += eachValue;
            isGraduation = NO;
        }
        else
        {
            isGraduation = YES;
            startY += eachValue * 1.5;
        }
    }
    self.trackLayerGraduationYellow.path = pointerOutsideYellow.CGPath;
    
    UIBezierPath * pointerOutsideGreen = [UIBezierPath bezierPath];
    startY = eachValue * 0.5f;
    while (startY < _trackLayerGraduationGreen.frame.size.height - eachValue)
    {
        if(isGraduation)
        {
            NSNumber * startNumberY = [NSNumber numberWithFloat:startY];
            [_arrayStartY addObject:startNumberY];
            [pointerOutsideGreen moveToPoint:CGPointMake(0, startY)];
            
            [pointerOutsideGreen addLineToPoint:CGPointMake(_trackLayerGraduationGreen.frame.size.width, startY)];
            startY += eachValue;
            isGraduation = NO;
        }
        else
        {
            isGraduation = YES;
            startY += eachValue * 1.5;
        }
    }
    self.trackLayerGraduationGreen.path = pointerOutsideGreen.CGPath;
}

-(void) updateTrackLayerReality
{
    _trackLayerRealityYerrow.lineWidth = _backgroundView2.frame.size.height/108;
    _trackLayerRealityGreen.lineWidth = _backgroundView2.frame.size.height/108;
    UIBezierPath * pointerOutsideGreen = [UIBezierPath bezierPath];
    
    NSInteger length = [_arrayStartY count];
    int indexFloat = 41;
    int i = 0;
    
    BOOL isHaveYellow = length * _value > 29;

    for(; i < (isHaveYellow ? 29 :length * _value); ++i)
    {
        NSNumber * nowFloatY = [_arrayStartY objectAtIndex:indexFloat];
        [pointerOutsideGreen moveToPoint:CGPointMake(0, nowFloatY.floatValue)];
        
        [pointerOutsideGreen addLineToPoint:CGPointMake(_trackLayerGraduationGreen.frame.size.width, nowFloatY.floatValue)];
        indexFloat--;
    }
    self.trackLayerRealityGreen.path = pointerOutsideGreen.CGPath;
    
    
    UIBezierPath * pointerOutsideYellow = [UIBezierPath bezierPath];
    for(; i < length * _value; ++i)
    {
        NSNumber * nowFloatY = [_arrayStartY objectAtIndex:indexFloat];
        [pointerOutsideYellow moveToPoint:CGPointMake(0, nowFloatY.floatValue)];
            
        [pointerOutsideYellow addLineToPoint:CGPointMake(_trackLayerGraduationGreen.frame.size.width, nowFloatY.floatValue)];
        indexFloat--;
    }
    self.trackLayerRealityYerrow.path = pointerOutsideYellow.CGPath;
}
-(void) createKnobUI
{
    [self addSubview:_backgroundView2];
    [self.layer addSublayer:_trackLayerGraduationYellow];
    [self.layer addSublayer:_trackLayerGraduationGreen];
    [self.layer addSublayer:_trackLayerRealityGreen];
    [self.layer addSublayer:_trackLayerRealityYerrow];
    [self addSubview:_backgroundView];
}

-(void)setValue:(CGFloat)value
{
    if(_value != value)
    {
        _value = value;
        [self updateTrackLayerReality];
    }
}

@end
